﻿#region  文件信息
// ---------------------------------------------------------------------------------------
// <copyright file="Encryption.cs" company="">
// 作者：李帅伦
// </copyright>
// <summary>
// 类型简介：
// 类型详细：Encryption.cs
// 创建时间：2014-12-23 14:21
// 修改时间：2014-12-23 14:21
// </summary>
// ---------------------------------------------------------------------------------------
#endregion

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Web.Security;

namespace LsTool
{
    /// <summary>
    /// 加密解密       
    /// </summary>
    public class EncryptionCom
    {
        static string Hash(HashAlgorithm md5Hasher, string str)
        {
            // Create a new instance of the MD5CryptoServiceProvider object.
            //HashAlgorithm md5Hasher = new SHA1CryptoServiceProvider();

            // Convert the input string to a byte array and compute the hash.
            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(str));

            // Create a new Stringbuilder to collect the bytes
            // and create a string.
            StringBuilder sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data 
            // and format each one as a hexadecimal string.
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            return sBuilder.ToString().ToUpper();
        }

        /// <summary>
        /// sha1加密
        /// </summary>
        /// <param name="str">文本</param>
        /// <returns></returns>
        public static string Sha1(string str)
        {
            var hashPasswordForStoringInConfigFile = FormsAuthentication.HashPasswordForStoringInConfigFile(str, "SHA1");
            if (hashPasswordForStoringInConfigFile != null)
            {
                return hashPasswordForStoringInConfigFile.ToLower();
            }
            else
            {
                return "";
            }
        }

        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="str">文本</param>
        /// <returns></returns>
        public static string Md532(string str)
        {
            var hashPasswordForStoringInConfigFile = FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5");
            if (hashPasswordForStoringInConfigFile != null)
            {
                return hashPasswordForStoringInConfigFile.ToUpper();
            }
            else
            {
                return "";
            }
        }

        /// <summary>
        /// des加密
        /// </summary>
        /// <param name="sourceString">
        /// 文本
        /// </param>
        /// <param name="key">
        /// 密钥
        /// </param>
        /// <param name="iv">
        /// 向量
        /// </param>
        /// <returns>
        /// </returns>
        public static string DesEncryption(string sourceString, string key, string iv)
        {
            try
            {
                byte[] btKey = Encoding.UTF8.GetBytes(key);

                byte[] btIV = Encoding.UTF8.GetBytes(iv);

                DESCryptoServiceProvider des = new DESCryptoServiceProvider();

                using (MemoryStream ms = new MemoryStream())
                {
                    byte[] inData = Encoding.UTF8.GetBytes(sourceString);
                    try
                    {
                        using (
                            CryptoStream cs = new CryptoStream(
                                ms,
                                des.CreateEncryptor(btKey, btIV),
                                CryptoStreamMode.Write))
                        {
                            cs.Write(inData, 0, inData.Length);

                            cs.FlushFinalBlock();
                        }

                        return Convert.ToBase64String(ms.ToArray());
                    }
                    catch
                    {
                        return sourceString;
                    }
                }
            }
            catch
            { 
                return "DES加密出错";
            } 
        }

        /// <summary>
        /// des解密
        /// </summary>
        /// <param name="encryptedString">
        /// 文本
        /// </param>
        /// <param name="key">
        /// 密钥
        /// </param>
        /// <param name="iv">
        /// 向量
        /// </param>
        /// <returns>
        /// </returns>
        public static string DesDecrypt(string encryptedString, string key, string iv)
        {

            byte[] btKey = Encoding.UTF8.GetBytes(key);

            byte[] btIV = Encoding.UTF8.GetBytes(iv);

            DESCryptoServiceProvider des = new DESCryptoServiceProvider();

            using (MemoryStream ms = new MemoryStream())
            {
                byte[] inData = Convert.FromBase64String(encryptedString);
                try
                {
                    using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write))
                    {
                        cs.Write(inData, 0, inData.Length);

                        cs.FlushFinalBlock();
                    }

                    return Encoding.UTF8.GetString(ms.ToArray());
                }
                catch
                {
                    return encryptedString;
                }
            }  
        }
    }
}